package com.timetable.model;

import java.util.*;
//import java.sql.*;
import com.driver.model.*;
import com.carstop.model.*;
import com.passenger.model.*;


public class TimeTableDAO {
	private List<TimeTableVO> timeTableList;
	
	public TimeTableDAO() {
		this.timeTableList = getAllPathDetail();
	}

	public List<TimeTableVO> getTimeTableList() {
		return timeTableList;
	}

	public void setTimeTableList(List<TimeTableVO> timeTableList) {
		this.timeTableList = timeTableList;
	}

	public List<TimeTableVO> getAllPathDetail() {
		List<TimeTableVO> timeTableList = new ArrayList<TimeTableVO>();
		List<CarDriverPathVO> drvList = null; 
		List<CarStopDetailVO> stopDetailList =null; 
		List<CarStopPassengerVO> passengerList = null;
		CarDriverPathService drvPathSvc = new CarDriverPathService();
		TimeTableVO timeTableVO = null;
		CarStopDetailDAO carstopDetailDao = new CarStopDetailDAO();
		CarStopDAO carstopDao = new CarStopDAO();
		CarStopDetailVO stopDetail = null;
		CarStopVO carstop = null;
		CarStopPassengerVO passenger = null;
		String pathId = null, driver = null;
		
		drvList = drvPathSvc.getAll();
	
		CarDriverPathVO drv = null;
		for (int i = 0; i < drvList.size(); i++) {
			drv = drvList.get(i);
			pathId = drv.getDriver_path_id();
			driver = drv.getOwner();
			stopDetailList = drv.getStopDetailList();
			for (int j = 0; j < stopDetailList.size() - 1; j++) {
				TimeTableVO timeTab = new TimeTableVO();
				timeTab.setPathId(pathId);
				timeTab.setDriver(driver);
				stopDetail = stopDetailList.get(j);
				carstop = stopDetail.getCarStop();
				timeTab.setFrom(carstop.getName());
				timeTab.setDepartureTime(stopDetail.getArrival());
				passengerList = stopDetail.getPassenger();
				timeTab.setPassenger(passengerList);
				
				stopDetail = stopDetailList.get(j+1);
				carstop = stopDetail.getCarStop();
				timeTab.setTo(carstop.getName());
				timeTab.setArrivalTime(stopDetail.getArrival());
				
				timeTableList.add(timeTab);
			}
		}
		
		showTimeTable(timeTableList);
		
		return timeTableList;
	}
	
	private void showTimeTable(List<TimeTableVO> timeTableList) {
		TimeTableVO timeTab = null;
		CarStopPassengerVO passenger = null;
		List<CarStopPassengerVO> passengerList = null;
		
		for (int i = 0; i < timeTableList.size(); i++) {
			timeTab = timeTableList.get(i);
			
			System.out.print(timeTab.getPathId());
			System.out.print(timeTab.getFrom());
			System.out.print(timeTab.getDepartureTime());
			System.out.print(timeTab.getTo());
			System.out.print(timeTab.getArrivalTime());
			
			passengerList = timeTab.getPassenger();
			if (passengerList == null) {
				System.out.println();
				continue;
			}
			for (int j = 0; j < passengerList.size(); j++) {
				passenger = passengerList.get(j);
				System.out.printf("%s(%s)", passenger.getName(), passenger.getMemberId());
			}
			System.out.println();
		}
		
	}
}